﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using TCKQKL.FrontEnd.DataModel;
using TCKQKL.Service.LookupClient;
using System.Collections.Generic;

namespace TCKQKL.FrontEnd.Web
{
    public partial class Order : System.Web.UI.Page
    {
        public List<OrdersOnlineUI> orderList = new List<OrdersOnlineUI>();

        protected void Page_Load(object sender, EventArgs e)
        {
            LSClient client = new LSClient();
            InvestorDataProfileUI investor = client.getAccountProfileData(client.getAccountData(HttpContext.Current.User.Identity.Name).investorId);
            client.Close();

            if (investor != null)
            {
                if (!investor.ServiceRegistered.Equals("ALL"))
                    Response.Redirect("./Error.aspx?Message=998");
                else
                {
                    if (Page.Master != null)
                    {
                        Label fullName = (Label)Page.Master.FindControl("lblFullName");
                        Label accountNo = (Label)Page.Master.FindControl("lblAccountNo");
                        Label phone = (Label)Page.Master.FindControl("lblPhone");
                        Label address = (Label)Page.Master.FindControl("lblAddress");
                        fullName.Text = investor.name;
                        accountNo.Text = investor.accountNo;
                        address.Text = investor.address;
                        phone.Text = investor.phone;
                    }
                    client = new LSClient();
                    orderList = client.getOrderListByStatus("");
                    client.Close();
                    rptOrders.DataSource = orderList;
                    rptOrders.DataBind();
                }
            }
        }

        protected void wzdOrder_FinishButtonClick(object sender, WizardNavigationEventArgs e)
        {
            Response.Redirect("./Order.aspx");
        }

        protected void wzdOrder_ActiveStepChanged(object sender, EventArgs e)
        {
            if (wzdOrder.ActiveStepIndex == 1)
            {
                HiddenField hf = (HiddenField)ucOrderUI.FindControl("hfOrderId");
                int orderNum = (!string.IsNullOrEmpty(hf.Value)) ? Convert.ToInt32(hf.Value) : 0;

                if (!orderNum.Equals(0))
                {
                    LSClient client = new LSClient();
                    OrdersOnlineUI original = client.GetOrderByOrderId(orderNum);
                    client.Close();
                    ucOrderConfim.Submited = original;
                }
                else
                {
                    ucOrderConfim.Submited = ucOrderUI.SubmitedOrder;
                }

            }
        }

        protected void wzdOrder_NextButtonClick(object sender, WizardNavigationEventArgs e)
        {
            int result = 0;
            try
            {
                HiddenField hf = (HiddenField)ucOrderUI.FindControl("hfOrderId");
                int orderNum = (!string.IsNullOrEmpty(hf.Value)) ? Convert.ToInt32(hf.Value) : 0;

                LSClient client = new LSClient();
                OrdersOnlineUI original = client.GetOrderByOrderId(orderNum);

                if (!orderNum.Equals(0))
                {

                    TextBox newPrice = (TextBox)ucOrderUI.FindControl("txtNewPrice");
                    result = client.ChangeOrder(new OrdersOnlineUI(0, original.AccountNo, original.OrderSide, original.Symbol.ToUpper(), original.OrderSpec, original.OrderClass, original.OrderMode, DateTime.Now, original.ExpireDate, "", DateTime.Now, (original.OrderClass.Equals("LO")) ? original.Price : original.OrderClass, original.Quantity, original.OrderType),
                                                new OrdersOnlineUI(0, original.AccountNo, original.OrderSide, original.Symbol.ToUpper(), original.OrderSpec, original.OrderClass, original.OrderMode, DateTime.Now, original.ExpireDate, "", DateTime.Now, (original.OrderClass.Equals("LO")) ? newPrice.Text : original.OrderClass, original.Quantity, original.OrderType));
                }
                else
                    if (ucOrderUI.orderType.Equals("CHANGE"))
                    {
                        result = client.ChangeOrder(new OrdersOnlineUI(0, ucOrderUI.SubmitedOrder.AccountNo, ucOrderUI.SubmitedOrder.OrderSide, ucOrderUI.SubmitedOrder.Symbol.ToUpper(), ucOrderUI.SubmitedOrder.OrderSpec, ucOrderUI.SubmitedOrder.OrderClass, ucOrderUI.SubmitedOrder.OrderMode, DateTime.Now, ucOrderUI.SubmitedOrder.ExpireDate, "", DateTime.Now, (ucOrderUI.SubmitedOrder.OrderClass.Equals("LO")) ? ucOrderUI.SubmitedOrder.Price : ucOrderUI.SubmitedOrder.OrderClass, ucOrderUI.SubmitedOrder.Quantity, ucOrderUI.SubmitedOrder.OrderType),
                                                    new OrdersOnlineUI(0, ucOrderUI.SubmitedOrder.AccountNo, ucOrderUI.SubmitedOrder.OrderSide, ucOrderUI.SubmitedOrder.Symbol.ToUpper(), ucOrderUI.SubmitedOrder.OrderSpec, ucOrderUI.SubmitedOrder.OrderClass, ucOrderUI.SubmitedOrder.OrderMode, DateTime.Now, ucOrderUI.SubmitedOrder.ExpireDate, "", DateTime.Now, (ucOrderUI.SubmitedOrder.OrderClass.Equals("LO")) ? ucOrderUI.newPrice : ucOrderUI.SubmitedOrder.OrderClass, ucOrderUI.SubmitedOrder.Quantity, ucOrderUI.SubmitedOrder.OrderType));

                    }
                    else
                        if (ucOrderUI.orderType.Equals("PLACE"))
                        {
                            OrdersOnlineUI order = new OrdersOnlineUI(0, ucOrderUI.SubmitedOrder.AccountNo, ucOrderUI.SubmitedOrder.OrderSide, ucOrderUI.SubmitedOrder.Symbol.ToUpper(), ucOrderUI.SubmitedOrder.OrderSpec, ucOrderUI.SubmitedOrder.OrderClass, ucOrderUI.SubmitedOrder.OrderMode, DateTime.Now, ucOrderUI.SubmitedOrder.ExpireDate, "", DateTime.Now, (ucOrderUI.SubmitedOrder.OrderClass.Equals("LO")) ? ucOrderUI.SubmitedOrder.Price : ucOrderUI.SubmitedOrder.OrderClass, ucOrderUI.SubmitedOrder.Quantity, ucOrderUI.SubmitedOrder.OrderType);
                            result = client.PlaceOrder(order);
                        }
                        else
                            if (ucOrderUI.orderType.Equals("CANCEL"))
                            {
                                result = client.CancelOrder(new OrdersOnlineUI(0, ucOrderUI.SubmitedOrder.AccountNo, ucOrderUI.SubmitedOrder.OrderSide, ucOrderUI.SubmitedOrder.Symbol.ToUpper(), ucOrderUI.SubmitedOrder.OrderSpec, ucOrderUI.SubmitedOrder.OrderClass, ucOrderUI.SubmitedOrder.OrderMode, DateTime.Now, ucOrderUI.SubmitedOrder.ExpireDate, "", DateTime.Now, (ucOrderUI.SubmitedOrder.OrderClass.Equals("LO")) ? ucOrderUI.SubmitedOrder.Price : ucOrderUI.SubmitedOrder.OrderClass, ucOrderUI.SubmitedOrder.Quantity, ucOrderUI.SubmitedOrder.OrderType));
                            }
                client.Close();

            }
            catch
            {
                Response.Redirect("./Error.aspx");
            }
            if (result != 0)
                Response.Redirect("./Error.aspx?Message=" + result.ToString(), true);

        }

        public void UpdateSelectedOrder(OrdersOnlineUI order)
        {
            ucOrderUI.order = order;
            ucOrderUI.Update();
        }

        protected void tmRefreshList_Tick(object sender, EventArgs e)
        {
            LSClient client = new LSClient();
            orderList = client.getOrderListByStatusAndAccountNo("", HttpContext.Current.User.Identity.Name.Trim());
            client.Close();
            rptOrders.DataSource = orderList;
            rptOrders.DataBind();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            string temp = "";
            int result = 0;
            LSClient client = new LSClient();
            foreach (DataListItem item in rptOrders.Items)
            {
                CheckBox chkCancel = (CheckBox)item.FindControl("matched");
                if (chkCancel.Checked)
                {
                    try
                    {
                        string accNo = HttpContext.Current.User.Identity.Name;
                        string symbol = ((Label)item.FindControl("lblSymbol")).Text;
                        string orderSide = ((Label)item.FindControl("lblOrderSide")).Text;
                        string orderClass = ((Label)item.FindControl("lblOrderClass")).Text;
                        string orderSpec = "NORMAL";
                        int qtty = Convert.ToInt32(((Label)item.FindControl("lblQuantity")).Text);
                        string price = ((Label)item.FindControl("lblPrice")).Text;
                        string orderMode = "INTERNET";
                        result = client.CancelOrder(new OrdersOnlineUI(0,
                                                    accNo, orderSide, symbol, orderSpec, orderClass,
                                                    orderMode, DateTime.Now, DateTime.Today, "",
                                                    DateTime.Now, price, qtty, "CANCEL"));
                    }
                    catch
                    {
                        Response.Redirect("./Error.aspx");
                    }
                    if (result != 0)
                        Response.Redirect("./Error.aspx?Message=" + result.ToString(), true);
                    else
                        Response.Redirect("./Order.aspx");
                }
            }
        }

        protected void rptOrders_ItemCommand(object source, DataListCommandEventArgs e)
        {
            if (e.CommandName == "Change")
            {
                //CustomerDataList.SelectedIndex = e.Item.ItemIndex;
                //e.CommandArgument;
                LSClient client = new LSClient();
                OrdersOnlineUI selected = client.GetOrderByOrderId(Convert.ToInt32(e.CommandArgument));
                ucOrderUI.order = selected;
                ucOrderUI.Update();

            }
        }

        protected void lbBuy_Click(object sender, EventArgs e)
        {
            ucOrderUI.orderType = "PLACE";
            DropDownList orderType = (DropDownList)ucOrderUI.FindControl("ddlOrderType");
            orderType.SelectedValue = "PLACE";

            DropDownList side = (DropDownList)ucOrderUI.FindControl("ddlOrderSide");
            side.SelectedValue = "BUY";
            ucOrderUI.Update();
        }

        protected void lbSell_Click(object sender, EventArgs e)
        {
            ucOrderUI.orderType = "PLACE";
            DropDownList orderType = (DropDownList)ucOrderUI.FindControl("ddlOrderType");
            orderType.SelectedValue = "PLACE";

            DropDownList side = (DropDownList)ucOrderUI.FindControl("ddlOrderSide");
            side.SelectedValue = "SELL";
            ucOrderUI.Update();
        }

        protected void lbChange_Click(object sender, EventArgs e)
        {
            ucOrderUI.orderType = "CHANGE";
            DropDownList orderType = (DropDownList)ucOrderUI.FindControl("ddlOrderType");
            orderType.SelectedValue = "CHANGE";

            ucOrderUI.Update();
        }

        protected void lbCancel_Click(object sender, EventArgs e)
        {
            DropDownList orderType = (DropDownList)ucOrderUI.FindControl("ddlOrderType");
            orderType.SelectedValue = "CANCEL";
            ucOrderUI.Update();
        }

    }
}
